from django.contrib.auth.models import AbstractUser
from ops.utils.models import table_prefix
from dvadmin.utils.models import CoreModel
from django.db import models
from ops.config.models import Project

ENV_CHOICES = (
    ('pre', "灰度环境"),
    ('prod', "正式环境"),
)


class Notification(CoreModel):
    sender = models.CharField(max_length=100, blank=True, null=True, verbose_name="发送者", help_text="发送者")
    receivers = models.CharField(max_length=2000, blank=True, null=True, verbose_name="接收者", help_text="接收者")
    title = models.CharField(max_length=100, blank=True, null=True, verbose_name="标题", help_text="标题")
    content = models.CharField(max_length=2000, blank=True, null=True, verbose_name="内容", help_text="内容")
    type = models.CharField(max_length=100, blank=True, null=True, verbose_name="通知类型", help_text="通知类型")
    project = models.CharField(max_length=100, blank=True, null=True, verbose_name="通知项目", help_text="通知项目")
    sort = models.IntegerField(default=1, verbose_name="显示排序", null=True, blank=True, help_text="显示排序")
    remark = models.CharField(max_length=2000, blank=True, null=True, verbose_name="备注", help_text="备注")
    has_sql = models.CharField(max_length=100, default='否', verbose_name="有否有sql", help_text="有否有sql")

    class Meta:
        db_table = table_prefix + 'devops_notification'
        verbose_name = "信息通知"
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class Module(CoreModel):
    module_name = models.CharField(max_length=100, blank=True, null=True, verbose_name="模块名", help_text="模块名")
    alias_name = models.CharField(max_length=100, blank=True, null=True, verbose_name="模块别名", help_text="模块别名")
    params = models.CharField(max_length=2000, blank=True, null=True, verbose_name="发布参数", help_text="发布参数")
    version = models.CharField(max_length=100, blank=True, null=True, verbose_name="版本号", help_text="版本号")
    project = models.ForeignKey(to=Project, on_delete=models.SET_DEFAULT, default=None)
    sort = models.IntegerField(default=1, verbose_name="显示排序", null=True, blank=True, help_text="显示排序")

    class Meta:
        db_table = table_prefix + 'devops_module'
        verbose_name = "模块信息"
        verbose_name_plural = verbose_name
        ordering = ('sort',)


class Deploy(CoreModel):
    project = models.IntegerField(default=1, verbose_name="项目id", null=True, blank=True, help_text="项目id")
    module_name = models.CharField(max_length=100, blank=True, null=True, verbose_name="模块", help_text="模块")
    env = models.CharField(max_length=10, choices=ENV_CHOICES, default='default', verbose_name="发布环境", help_text="发布环境")

    version = models.CharField(max_length=100, blank=True, null=True, verbose_name="版本号", help_text="版本号")

    class Meta:
        db_table = table_prefix + 'devops_deploy'
        verbose_name = "发布信息"
        verbose_name_plural = verbose_name


class Domain(CoreModel):
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name="域名", help_text="域名")
    status = models.CharField(max_length=100, blank=True, null=True, verbose_name="模块", help_text="模块")

    class Meta:
        db_table = table_prefix + 'devops_domain'
        verbose_name = "域名"
        verbose_name_plural = verbose_name


class DomainSet(CoreModel):
    domain = models.CharField(max_length=100, blank=True, null=True, verbose_name="域名", help_text="域名")
    status = models.CharField(max_length=100, blank=True, null=True, verbose_name="状态", help_text="状态")
    type = models.CharField(max_length=100, blank=True, null=True, verbose_name="记录类型", help_text="记录类型")
    ttl = models.IntegerField(default=1, verbose_name="ttl值", null=True, blank=True, help_text="ttl值")
    records = models.CharField(max_length=2000, blank=True, null=True, verbose_name="记录值", help_text="记录值")

    class Meta:
        db_table = table_prefix + 'devops_domain_set'
        verbose_name = "域名解析记录"
        verbose_name_plural = verbose_name


class Cdn(CoreModel):
    domain = models.CharField(max_length=100, blank=True, null=True, verbose_name="域名", help_text="域名")
    business_type = models.CharField(max_length=100, blank=True, null=True, verbose_name="业务类型", help_text="业务类型")
    service_area = models.CharField(max_length=100, blank=True, null=True, verbose_name="服务范围", help_text="服务范围")
    origin_type = models.CharField(max_length=100, blank=True, null=True, verbose_name="源站类型", help_text="源站类型")
    ip_or_domain = models.CharField(max_length=100, blank=True, null=True, verbose_name="源站地址", help_text="源站地址")
    status = models.CharField(max_length=100, blank=True, null=True, verbose_name="状态", help_text="状态")

    cname = models.CharField(max_length=100, blank=True, null=True, verbose_name="CNAME", help_text="CNAME")

    class Meta:
        db_table = table_prefix + 'devops_cdn'
        verbose_name = "cdn表格"
        verbose_name_plural = verbose_name


class Ref(CoreModel):
    operator = models.CharField(max_length=20, blank=True, null=True, verbose_name="操作者", help_text="操作者")
    cache_type = models.CharField(max_length=100, blank=True, null=True, verbose_name="缓存类型", help_text="缓存类型")
    file_type = models.CharField(max_length=100, blank=True, null=True, verbose_name="文件类型", help_text="文件类型")
    url = models.CharField(max_length=100, blank=True, null=True, verbose_name="域名", help_text="域名")
    status = models.CharField(max_length=100, blank=True, null=True, verbose_name="状态", help_text="状态")

    class Meta:
        db_table = table_prefix + 'devops_cdn_ref_history'
        verbose_name = "cdn缓存刷新历史"
        verbose_name_plural = verbose_name
